﻿/// <reference path="../Controls/BaseComplexValueControl.js" />

var $FileUpload = TA.Turbo.Controls.FileUpload = function FileUpload(clientId)
{
	this.BaseComplexValueControl(clientId);
	$FileUpload.Instances[clientId] = this;
}
$FileUpload.Inherit($BaseComplexValueControl);

var _____a;
if (_____a)
{
	TA.Turbo.Controls.FileUpload.prototype = new TA.Turbo.Controls.BaseComplexValueControl();
	TA.Turbo.Controls.FileUpload.prototype.BaseComplexValueControl = new TA.Turbo.Controls.BaseComplexValueControl();
}


$FileUpload.Instances = new Object();
$FileUpload._rgxToken = new RegExp("t=(\\d+)");

$FileUpload.prototype._callBack = null;
$FileUpload.prototype._iframeWindow = null;
$FileUpload.prototype._iframe = null;

$FileUpload.prototype.get_AppRootUrl = function get_AppRootUrl()
{
	return this.get_Control().getAttribute("AppRootUrl");
}
$FileUpload.prototype.get_Iframe = function get_Iframe()
{
	if (this._iframe == null)
	{
		var cn = this.get_Control().childNodes, cnl = cn.length;
		for (var iElem = 0; iElem < cnl; ++iElem)
		{
			if (cn[iElem].tagName == "IFRAME")
			{
				this._iframe = cn[iElem];
				break;
			}
		}
	}
	return this._iframe;
}
$FileUpload.prototype.get_IframeWindow = function get_IframeWindow()
{
	if (this._iframeWindow == null)
	{
		this._iframeWindow = this.get_Iframe().contentWindow;
	}
	return this._iframeWindow;
}
$FileUpload.prototype.get_IframeInput = function get_IframeInput()
{
	return this.get_IframeWindow().document.getElementById("file");
}
$FileUpload.prototype.get_Token = function get_Token()
{
	return $FileUpload._rgxToken.exec(this.get_Iframe().src)[1];
}
$FileUpload.prototype.get_Value = function get_Value()
{
	if (this.get_IframeInput() != null)
		return this.get_IframeInput().value;
	return "";
}
$FileUpload.prototype.get_SerializedValue = function get_SerializedValue()
{
	return this.get_Token();
}
$FileUpload.prototype.set_SerializedValue = function set_SerializedValue(value)
{
	Throw("Can not set any serialized value on FileUpload controls. To clear it, assign null or empty string to the Value property.");
}
$FileUpload.prototype.get_Value = function get_Value()
{
	return this.get_IframeInput().value;
}
$FileUpload.prototype.set_Value = function set_Value(value)
{
	if (!(value == null || value == ""))
	{
		Throw("Can not set any value on FileUpload controls other than null/empty string (which clears it).");
	}
	this.get_IframeInput().value = "";
}

$FileUpload.prototype.Init = function Init()
{
	this.BaseComplexValueControl.Init.call(this);

	if (this.get_Iframe() == null)
	{ // INPUT-FILE was rendered because server thought JS might not be available
		this.get_Control().ClearChildNodes();

		var token = Math.round(Math.random() * 2000000000);
		var iframeUrl = this.get_AppRootUrl() + "TurboFileUpload.axd?i=" + this.get_Control().id + "&t=" + token;

		var iframe = this._iframe = document.createElement("iframe");
		iframe.style.width = "220px";
		iframe.style.height = "26px";
		iframe.frameBorder = "0";
		iframe.scrolling = "no";
		iframe.allowTransparency = "yes";
		this.get_Control().appendChild(iframe);

		iframe.AddEvent("load", this._iframe_Load, this);
		iframe.src = iframeUrl;
	}
	else
	{
		this._BindChangeEvent();
	}

	this.get_Control()["_C"] = this._Complete.bind(this);
}
$FileUpload.prototype._iframe_Load = function _iframe_Load()
{
	this._BindChangeEvent();
}
$FileUpload.prototype._BindChangeEvent = function _BindChangeEvent()
{
	if (this.get_IframeInput() == null)
	{
		setTimeout(this._BindChangeEvent.bind(this), 200);
		return;
	}
	
	this.get_IframeInput().onchange = this._input_Change.bind(this);
}
$FileUpload.prototype._input_Change = function _input_Change()
{
	this._OnValueChanged();
}
$FileUpload.prototype.GetProgress = function GetProgress(callback)
{
	var rgx = new RegExp("[^?]+");
	var handlerUrl = rgx.exec(this.get_Iframe().src)[0];
	Ajax.Get(handlerUrl,
	{
		"t": this.get_Token(),
		"s": 1
	}, this.GetProgress_CallBack.bind(this, callback));
}
$FileUpload.prototype.GetProgress_CallBack = function GetProgress_CallBack(callback, responseText)
{
	var fup = new $FileUploadProgress();
	fup.Deserialize(responseText);
	callback(fup);
}
$FileUpload.prototype._Submit = function Submit(callBack)
{
	this._callBack = callBack;
	this.get_IframeWindow().document.getElementById("file").style["display"] = "none";
	this.get_IframeWindow().document.getElementById("spanProgress").style["display"] = "";
	this.get_IframeWindow().document.getElementById("button").click();
}

$FileUpload.prototype._Complete = function _Complete()
{
	if (this._callBack != null)
	{
		this._callBack(this);
		this._callBack = null;
	}
	this._BindChangeEvent();
}



